/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.model;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Vector;

import be.gnx.ganesa.application.event.LayerCreatedEvent;
import be.gnx.ganesa.application.event.LayerCreatedListener;
import be.gnx.ganesa.application.plugin.ImportPlugin;
import be.gnx.ganesa.application.ui.dialog.ImportDialog.ImportDialogResult;

public class Project {

  public boolean saved = true;

  public LinkedHashMap<String, Layer> layers = new LinkedHashMap<String, Layer>();

  private Layer selectedLayer;

  private Cuboid zoomBox = new Cuboid(new Coordinate(-(Math.PI / 2), -Math.PI, 0), new Coordinate(Math.PI / 2, Math.PI, 0));

  protected Project() {
    
  }

  public boolean isSaved() {
    return saved;
  }

  public <LayerClazz extends Layer> Vector<LayerClazz> newLayers(ImportPlugin importPlugin, ImportDialogResult importDialogResult) {
    Vector<LayerClazz> newLayers = importPlugin.newLayers(importDialogResult);
    for (Layer layer : newLayers) {
      String name = layer.getName();
      Integer extension = null;
      while (layers.containsKey(nameWithExtension(name, extension))) {
        if (extension == null) {
          extension = 0;
        }
        extension++;
      }
      layer.setName(nameWithExtension(name, extension));
      layers.put(nameWithExtension(name, extension), layer);
      selectedLayer = layer;
      for (LayerCreatedListener layerCreatedListener : layerCreatedListeners) {
        layerCreatedListener.layerCreatedEventhappened(new LayerCreatedEvent(layer));
      }
    }
    saved = false;
    return newLayers;
  }

  public Collection<Layer> getLayers() {
    return layers.values();
  }

  public Layer getSelectedLayer() {
    return selectedLayer;
  }

  public void setSelectedLayer(Layer layer) {
    this.selectedLayer = layer;
  }

  private static String nameWithExtension(String name, Integer extension) {
    return name + ((extension != null) ? (" copy " + extension) : "");
  }

  public Cuboid getZoomBox() {
    return zoomBox;
  }

  public void setZoomBox(Cuboid zoomBox) {
    this.zoomBox = zoomBox;
  }

  /* event listeners */
  private Vector<LayerCreatedListener> layerCreatedListeners = new Vector<LayerCreatedListener>();

  public Vector<LayerCreatedListener> getLayerCreatedListeners() {
    return layerCreatedListeners;
  }

  public void addLayerCreatedListener(LayerCreatedListener layerCreatedListener) {
    if (layerCreatedListener == null) {
      throw new NullPointerException();
    } else {
      layerCreatedListeners.add(layerCreatedListener);
    }
  }

  public void removeLayerCreatedListener(
      LayerCreatedListener layerCreatedListener) {
    if (layerCreatedListener == null) {
      throw new NullPointerException();
    } else {
      layerCreatedListeners.remove(layerCreatedListener);
    }
  }

}
